import Express from "express"
import { addArtServ, findArtByIdServ, findArtByPageServ } from "../Service/articleServ.js";

export const articleRouter = Express.Router();


// Restful page, size, typeId, searchValue
// query? a=1&b=2
// /?size=5&page=1&searchValue=xxx&type=xxx
articleRouter.get('/',async(req,res)=>{
    const {page, size, searchValue, type} = req.query;
    const queryobj = {
    };
    if(searchValue){
        queryobj.title = searchValue;
    }
    if(type) {
        queryobj.typeId = type;
    }
    const result = await findArtByPageServ(page, size, queryobj)
    res.json(result)
})

articleRouter.post('/', async(req, res) => {
    const result = await addArtServ(req.body);
    res.json(result)
})

articleRouter.get("/:id",async(req,res)=>{
    const result = await findArtByIdServ(req.params.id)
    res.json(result)
})